<?php
// 动态获取当前可选医生信息

// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");
include_once("u_getLoginInfo.php");

//默认设置每位医生每天可预约人数为5人
$defined_num = 5;
//获取当前预约类型和日期
$type = $_GET['orderType'];
$date = $_GET['orderDate'];

// 查询当前预约类型和日期的医生信息
if ($type != "none" && $date != "") {
    $sql_doc =
        "SELECT *
        FROM doc_infos
        WHERE doc_Accept = '1'
        AND doc_TypeMain = '" . $type . "'";
    $result_doc = mysqli_query($connect_DB, $sql_doc);
    if ($result_doc) {
        echo "
            <label id='orderDocTips' class='form-label'><b>根据您的信息筛选出当前可预约医生>>></b></label><br>
        ";
        if (mysqli_num_rows($result_doc) == 0) {
            echo "
            <div class='form-check form-check-inline'>
            <input class='form-check-input' type='radio' name='docName' id='Radio1' disabled>
            <label class='form-check-label' for='Radio1'>当前暂无接诊医生</label>
            </div>
            ";
            exit();
        }
        while ($row_doc = mysqli_fetch_object($result_doc)) {
            $sql_isorder = "
                SELECT *
                FROM order_infos
                WHERE doc_ID = '" . $row_doc->doc_ID . "'
                AND orderDate = '" . $date . "'
            ";
            $result_isorder = mysqli_query($connect_DB, $sql_isorder);
            if ($result_isorder) {
                //获取当前医生已预约信息条数（即已经预约的人数）
                $row_isorder_num = mysqli_num_rows($result_isorder);
                // 判断医生是否可预约（预约次数是否小于设定默认次数）
                if ($row_isorder_num < $defined_num) {
                    $last_order_time = $defined_num - $row_isorder_num;
                    // 还有剩余预约名额则输出
                    echo "
                    <div class='form-check form-check-inline'>
                    <input class='form-check-input' type='radio' name='docID' id='Radio1' onclick='clickRadios()' value='" . $row_doc->doc_ID . "'>
                    <label class='form-check-label' for='Radio1'>" . $row_doc->doc_Name . " > 剩余 " . $last_order_time . " 位可约</label>
                    </div>
                    ";
                } else {
                    // 没有剩余预约名额则输出
                    echo "
                    <div class='form-check form-check-inline'>
                    <input class='form-check-input' type='radio' name='docName' id='Radio1' onclick='clickRadios()' value='" . $row_doc->doc_ID . "' disabled>
                    <label class='form-check-label' for='Radio1'>" . $row_doc->doc_Name . " > 已预约满</label>
                    </div>
                    ";
                }

            }

        }
    } else {
        echo "执行查询失败：<br>" . $sql_doc;
    }
} else {
    echo "
        <label id='orderDocTips' class='form-label'>选择 <b>预约服务类别</b>、<b>预约日期</b> 后，可选择预约医生</label>
        <br>
        <div class='form-check form-check-inline'>
        <label class='form-check-label'>当前不可选择</label>
    ";
}

$connect_DB->close();
?>